Repository navigation
Escape keyword-named identifiers in the Python generator [patch] - #347
Merged
Merged
Conversation
A field, property, parameter, local, function, class, enumeration member or alias named after a Python keyword came out verbatim, so the module failed with SyntaxError on import. An enumeration member called None, an ordinary C# name, was enough. Python now appends an underscore, the PEP 8 spelling for this, at each declaration, and references go through the same hook so they agree. The literal words True, False and None are left as written when a reference means them. Soft keywords (match, case, type) are legal names and are not escaped. Part of #106. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW6Nv9nDLvHQUanGyzZ
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Part of #106. This is the Python step of the split in the issue. Rust and C++ landed in #330. Go, JavaScript, C# and C are still open, so this PR deliberately doesn't close the issue.
What was wrong
PythonGeneratorwrote every declared name verbatim. A name that is a Python hard keyword made the module fail withSyntaxErroron import. An enumeration member calledNone, which is an ordinary C# name, was enough to trigger it, as were fields, parameters and locals namedfrom,lambda,in,is,passand so on.Change
PythonGeneratornow overridesReservedWordswith the hard keywords fromkeyword.kwlist. The soft keywords (match,case,type,_) are legal names, so they are left alone.EscapeIdentifier, which appends an underscore. That is a rename, and it is also PEP 8's spelling for this case.IsReceiverWordkeepsTrue,FalseandNoneas written when a reference means the literal.EscapeIdentifier:self.xinitialisers and member initialisersgloballists and keyword argumentsEscapeReference, so declarations and uses stay in step.Tests
ReservedWordIdentifierTestshas two new tests:Python_KeywordNamesRunAndResolvegenerates a class, an enum and a function whose names are Python keywords. It runs the module underpython3with a driver that reads each one back.Python_ReferenceKeepsTheLiteralWordsAndEscapesTheMemberchecks thatself.frombecomesself.from_while a bareNonestaysNone.Both tests fail with the generator change reverted (
SyntaxError: invalid syntax) and pass with it. The full suite passes: 1545 tests, 0 failed.Generated by Claude Code